<template>
  <div class="tab-bar-item" @click="itemClick">
    <!--  点击显示活跃图片 -->
    <div v-if="!isActive">
      <slot name="item-icon"></slot>
    </div>
    <!-- 不活跃的按钮不显活跃图片  -->
    <div v-else>
      <slot name="item-icon-active"></slot>
    </div>
    <!--  给活跃的按钮添加字体颜色 -->
    <div :class="{ active: isActive }">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  data() {
    return {};
  },
  computed: {
    // 是否点击了当前的tabbar按钮
    isActive() {
      // this.$router是当前活跃的路由,当前活跃的路由地址和当前点击的路由地址一致则返回ture
      return this.$route.path == this.path;
    },
  },
  // 3. 父传子
  props: {
    path: String,
  },
  methods: {
    // 1. 点击跳转路由
    itemClick() {
      this.$router.replace(this.path); // 2. 跳转地址由使用的父组件传入
    },
  },
};
</script>

<style scoped lang="less">
.tab-bar-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #eee;
  font-size: 15px;
  img {
    width: 22px;
    height: 22px;
    margin-bottom: 2px;
  }
  .active {
    color: #d9250e;
  }
}
</style>
